# CCNv2::Provisioning.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
use strict; use warnings;

package CCNv2::Provisioning;
use base qw(CCNCore::Provisioning);

# --- Tools ---

# --- Class initialization ---

sub initialize # Same parameters as CCN::Database::initialize
{
    my $self = shift;
    $self->SUPER::initialize(@_);

    # --- Identify which server we are running on

    my ($px_member, $cp_member, $mp_member) = 
    $self->sql_get( <<'SQL', $self->p->server_pk );
        SELECT px_member, cp_member, mp_member FROM server WHERE id = ?
SQL

    $self->{px_member} = $px_member;
    $self->{px_cluster} = $self->make_cluster_name($px_member,'px')
        if defined $px_member;
    
    $self->{cp_member} = $cp_member;
    $self->{cp_cluster} = $self->make_cluster_name($cp_member,'cp')
        if defined $cp_member;

    $self->{mp_member} = $mp_member;
    $self->{mp_cluster} = $self->make_cluster_name($mp_member,'mp')
        if defined $mp_member;

    my $vm_member = undef;
    
    $vm_member =
    $self->sql_get(
            q(SELECT cluster FROM cp_clusters WHERE id = ? AND provides_vm = 'TRUE'),
            $cp_member
    ) if defined $cp_member;

    $self->{vm_member} = $vm_member;
    $self->{vm_cluster} = $self->make_cluster_name($vm_member,'vm')
        if defined $vm_member;

    $self->{vmpath} = '/var/spool/asterisk/voicemail';
    $self->{sounds} = '/usr/share/asterisk/sounds';
}

sub is_a_file_server()
{
    my $self = shift;
    $self->{is_a_file_server} = $self->sql_get( <<'SQL', $self->p->server_pk ) > 0
        SELECT COUNT(*) FROM pool_server WHERE server = ? AND role = 'file'
SQL
        unless exists $self->{is_a_file_server};
    return $self->{is_a_file_server};
}

sub get_db_names
{
    my $self = shift;
    return ($self->SUPER::db_names,$self->get_px_db_names,$self->get_cp_db_names);
}

sub get_px_db_names()
{
    my $self = shift;

    return unless defined $self->p->{db_member} and $self->p->system_pk == $self->primary_db_server;

    return $self->sql_get_array(q(SELECT 'db_px_'||id FROM px_clusters WHERE db_cluster = ?),
        $self->p->{db_member});
    # If no record was found it means we are not the proper db_cluster.
}

sub get_cp_db_names()
{
    my $self = shift;

    return unless defined $self->p->{db_member} and $self->p->system_pk == $self->primary_db_server;

    return $self->sql_get_array(q(SELECT 'db_cp_'||id FROM cp_clusters WHERE db_cluster = ?),
        $self->p->{db_member});
    # If no record was found it means we are not the proper db_cluster.
}




sub dump_dbs()
{
    my $self = shift;
    $self->SUPER::dump_dbs();
    
    return if $self->bootstrapping;

    $self->sql_do_for(<<'SQL', $self->p->{db_member},
        SELECT db_name FROM provisioning WHERE db_cluster = ?
SQL
    sub
    {
        my $db = shift;

        $self->log("Backing up $db (provisioning)\n");

        use CCNCore::Provision::Database::CCNCore;
        my $k = new CCNCore::Provision::Database::CCNCore($self->p);
        unlink $k->dump_file();
        $k->dump($db) if $k->database_looksgood($db);
    });
}

sub restore_dbs()
{
    my $self = shift;
    $self->SUPER::restore_dbs();
    
    return if $self->bootstrapping;

    # Reprovision the databases that have been dropped/created
    $self->sql_do_for(<<'SQL', $self->p->{db_member},
        SELECT db_name FROM provisioning WHERE db_cluster = ?
SQL
    sub
    {
        my $db = shift;
        my $type = shift;

        $self->log("Restoring $db ($type)\n");

        use CCNCore::Provision::Database::CCNCore;
        my $k = new CCNCore::Provision::Database::CCNCore($self->p);
        $k->reprovision($db);
    });
}

1;